

package com.sakila.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sakila.dbo.CountryDBO;
import com.sakila.util.DBConnection;


public class CountryDAO implements ICountryDAO {





	  private Logger logger = Logger.getLogger(this.getClass());
	//SimpleDateFormat formatter = new SimpleDateFormat(
		//	"yyyy.MM.dd G 'at' HH:mm:ss z");

	/* (non-Javadoc)
	 * @see com.sakila.dao.ICountryDAO#findById(int)
	 */
	@Override
	public CountryDBO findById(int countryId) {
		CountryDBO dbo = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select country_id as countryId, country as country, last_update as lastUpdate");
			sb.append("from country ");
			sb.append("where country_id = " + countryId);
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here in Actor DAO: "+ sb.toString());
			rs = statement.executeQuery();
			if (rs.next()) {
				dbo = new CountryDBO();
				dbo.setCountry_id(rs.getInt("countryId"));
				dbo.setCountry(rs.getString("country"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				
				
				
			}

		} catch (SQLException e) {
			logger.error("Exception in Country DAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
		return dbo;
	}

	/* (non-Javadoc)
	 * @see com.sakila.dao.ICountryDAO#findAll()
	 */
	@Override
	public List<CountryDBO> findAll() {
		List<CountryDBO> returnList = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		CountryDBO dbo = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select country_id as countryId, country as country, last_update as lastUpdate");
			sb.append("from country ");
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here Country DAO infindAll: "+ sb.toString());
			rs = statement.executeQuery();
			returnList = new ArrayList<CountryDBO>();
			while (rs.next()) {
				dbo = new CountryDBO();
				dbo.setCountry_id(rs.getInt("countryId"));
				dbo.setCountry(rs.getString("country"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				returnList.add(dbo);
				
			}

		} catch (SQLException e) {
			logger.error("Exception in Country DAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
logger.debug("Display Me everyone from List:" + returnList);
		return returnList;
	}
	
}

